<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .main {
            width: 400px;
            margin: 0 auto;
            text-align: center;
        }
        .list {
            border: 1px solid #999;
            min-height: 400px;
            margin-bottom: 20px;
            text-align: left;
        }
        .fr {
        	float: right;
        }
    </style>
</head>

<body>
    <div class="main">
        <ol class="list" id="list">
			<li></li>	
        </ol>
        <input type="text" id="inp" placeholder="请输入留言" onkeypress="enter();">
        <input type="button" value="提交" onclick="submit();">
    </div>
</body>
</html>
<script src="js/util.js"></script>
<script>
    var inp = document.getElementById('inp');
    var list = document.getElementById('list');
    function submit() {
    		// 获取内容
    		var text = inp.value;
    		// 添加日期并格式化
    		var date = new Date();
    		var dateStr = formatDate2(date);
    		// 创建li节点
    		var li = document.createElement('li');
    		// 给li添加内容
			li.innerHTML = `<span>${text}</span><span class="fr">${dateStr}</span>`;
       		 // 把li添加到ul里面
       		 if (!text) {
       		 	alert('请输入留言');
       		 	return false;
       		 }
       		 list.appendChild(li);
       		 inp.value = '';
    }
    
    function enter() {
    	// 说明按了回车键
    	if (event.keyCode === 13) {
    		submit();
    	}
    }
</script>